#ifndef __UDSF_I2C_SLAVE_C_
#define __UDSF_I2C_SLAVE_C_


#include "udf_global.h"

#include "./udsf_gpio.c"
#include "./udsf_delay.c"
#include "./UDSF_uart.c"
#define SLAVE_ADDR 0x2E //写发送0x50 读发送0x51
#define ADDMAX     20

#define WAIT_IIC_SCL_HIGH   udsf_i2c_timeout(1)
#define WAIT_IIC_SCL_LOW    udsf_i2c_timeout(0)


#define SCL            GPIO0_7
#define SDA            GPIO0_6
#define GET_SCL_DAT    SCL
#define GET_SDA_DAT    SDA
#define SDA_OUT        GPIO->PMODE |= (1 << 6);
#define SDA_IN         GPIO->PMODE &= ~(1 << 6);
#define SET_SDA_LOW    SDA=0;
#define SET_SDA_HIGH   SDA=1;

#define IIC_SLAVE_SEND_LOW  {if(WAIT_IIC_SCL_LOW!=0){ SDA_OUT; SET_SDA_LOW; WAIT_IIC_SCL_HIGH;}}
#define IIC_SLAVE_SEND_HIGH {WAIT_IIC_SCL_LOW; SDA_OUT; SET_SDA_HIGH; WAIT_IIC_SCL_HIGH;}

#define IIC_SLAVE_SEND_ACK  IIC_SLAVE_SEND_LOW   //从机读取结束发送ACK
#define IIC_SLAVE_SEND_NAK  IIC_SLAVE_SEND_HIGH  //从机读取结束发送NACK

#define debug  0


#define PRE_TRIGGER_TIME      0 //预触发时间 状态
#define MEASURING_TIME        1 //检测时间 状态
#define RESPOND               2 //响应状态

//#define r_w_reg       paramRSV->rsv[1]
//#define SCL_old       paramRSV->rsv[2]
//#define SDA_old       paramRSV->rsv[3]


static unsigned int udsf_i2c_timeout(unsigned char PIN) __attribute__((unused));
static void         udsf_i2c_init(void) __attribute__((unused));
static void         udsf_i2c_salve_check_run(void) __attribute__((unused));

static unsigned int udsf_i2c_timeout(unsigned char PIN)//CLK
{
    //10000->1.2ms    1000->12x us
    uint32_t time = 10000;
    if (PIN == 1)
    {
        while ((!GET_SCL_DAT) && time) //wait SCL HI
        {
            time--;
        }
    }
    else
    {
        while (GET_SCL_DAT && time)
        {
            time--;

        }
    }
    return time;
}

static void udsf_i2c_init(void)
{
    /**/
//寄存器初值 初始化

    sUDF_global.i2c_reg00 = 0x00;
	
    sUDF_global.i2c_reg10 = 0x00;//out1
    sUDF_global.i2c_reg11 = 0x00;
    sUDF_global.i2c_reg12 = 0x00;//out2
    sUDF_global.i2c_reg13 = 0x00;
    sUDF_global.i2c_reg31 = 0x00;
    sUDF_global.i2c_reg32 = 0x00;
    sUDF_global.i2c_reg3e = 0x00;
    sUDF_global.i2c_reg3f = 0x00;	
    sUDF_global.i2c_reg41 = 0x01;
		sUDF_global.i2c_reg42 = 0x05;
		sUDF_global.i2c_reg43 = 0x00;
		sUDF_global.i2c_reg44 = 0x00;
		sUDF_global.i2c_reg45 = 0x00;
	
		sUDF_global.i2c_reg4e = 0x14;
		sUDF_global.i2c_reg4f = 0x00;
	
		sUDF_global.i2c_reg5c = 0x00;//灵敏度
	
		sUDF_global.i2c_reg68 = 0x68;
		sUDF_global.i2c_reg67 = 0x67;
		
		sUDF_global.i2c_regaa = 0xF0;
		for(int i=0;i<256;i++){
			sUDF_global.i2c_regbb[i] = 0x00;
		}
		
		sUDF_global.binpage_cnt = 0;
		
		sUDF_global.version[0] = 0x01;
		sUDF_global.version[1] = 0x02;
		sUDF_global.version[2] = 0x02;
		sUDF_global.version[3] = 0xAA;
		
	
#ifdef IIC_OUTPUT
	//IO 初始化
		USCI1->IMR |= 0x6;
		PINMUX->MUX0 = 0x00000000;

//    udsf_pinctrl_ae(GPIO_PIN_5, 0); // 数字
    udsf_pinctrl_ae(GPIO_PIN_6, 0); // 数字
    udsf_pinctrl_ae(GPIO_PIN_7, 0); // 数字

    udsf_pinctrl_od((1 << 6), 1);
    udsf_pinctrl_od((1 << 7), 1);
//    udsf_pinctrl_re((1 << 6), 1);
//    udsf_pinctrl_re((1 << 7), 1);
//    udsf_pinctrl_ue((1 << 6), 1);
//    udsf_pinctrl_ue((1 << 7), 1);
//输入
    GPIO->PMODE &= ~(1 << 6);//GPIO0.6 SDA
    GPIO->PMODE &= ~(1 << 7);//GPIO0.7 SCL
		// 双沿中断
    GPIO->INTTYPE &= ~(1 << 6);
    GPIO->INTPOL |= (2 << (6 << 1));
    GPIO->INTEN |= (1 << 6);
		GPIO->INTSTR = 1 << 6;
//    GPIO->INTTYPE &= ~(1 << 6);
//    GPIO->INTPOL |= (1 << (6 << 1));
//    GPIO->INTEN |= (1 << 6); //

    sUDF_global.start_f = 0;
#endif
}




static void udsf_i2c_salve_check_run_multidata(void)
{
///*I2C slave logic*/
//变量
    unsigned char bitcount, iic_slv_addr, receive_byte;
    uint8_t  sda_bit_val, r_w_reg;
    uint32_t time, time2;
    sUDF_global.debugCnt = 0;

    SDA_IN
//    if ((GPIO->INTSTR & (1 << 7)) != 0) //下降沿 ,阻塞，延时 等待下一个下降沿
  	if(sUDF_global.state==RESPOND)	
    {	

			/*连续地址唤醒*/
//        udsf_delay_us(600);
//			GPIO0_2 = 1;
        udsf_delay_us(100);//600us  need		      
        GPIO->INTSTR |= (1 << 6);
      	GPIO->INTPOL = (1 << (6 << 1));
        time = 24000*2;
	
        while (time)         //第二次通讯
        {
            SDA_IN
            if ((GPIO->INTSTR & (1 << 6)) != 0) //第二次下降沿
            {
							  GPIO->INTSTR |= (1 << 6);
                udsf_delay_us(1);
                if (SCL == 1)
                    sUDF_global.start_f = 1; //1;
            }

            if (sUDF_global.start_f == 1)  // first start
            {
                WDT->STR = 0x1;
                iic_slv_addr = 0;
                for (bitcount = 0; bitcount < 7; bitcount ++) //receive first  device addr
                {
                    WAIT_IIC_SCL_LOW;
                    WAIT_IIC_SCL_HIGH;
                    iic_slv_addr <<= 1;  //先移位，再读数

                    iic_slv_addr |= GET_SDA_DAT;
                }
                // 已经读取前7位地址 xxxx xxx0
                WAIT_IIC_SCL_LOW;
                WAIT_IIC_SCL_HIGH;

                IIC_SLAVE_SEND_ACK;

                if (iic_slv_addr == SLAVE_ADDR)
                {
                    receive_byte = 0;
                    for (bitcount = 0; bitcount < 8; bitcount ++)    //receive r/w register addr
                    {
                        WAIT_IIC_SCL_LOW;
                        SDA_IN;//输入
                        WAIT_IIC_SCL_HIGH;
                        receive_byte <<= 1;  //先移位，再读数

                        receive_byte |= GET_SDA_DAT;
                    }
                    r_w_reg = receive_byte;
                    IIC_SLAVE_SEND_ACK;                   //bit9

					if (	(r_w_reg != 16) 	&& (r_w_reg != 17)
						&&  (r_w_reg != 49) 	&& (r_w_reg != 50)
						&&  (r_w_reg != 62) 	&& (r_w_reg != 63)										
						&&  (r_w_reg != 65) 	&& (r_w_reg != 66)
						&&  (r_w_reg != 67) 	&& (r_w_reg != 68)
						&&  (r_w_reg != 69) 	&& (r_w_reg != 78)
						&&  (r_w_reg != 79) 	&& (r_w_reg != 92)
						&&  (r_w_reg != 0)  	&& (r_w_reg != 103)
						&&  (r_w_reg != 104)	&& (r_w_reg != 18)
						&&  (r_w_reg != 19) 	&& (r_w_reg != 0xaa) 
						&&  (r_w_reg != 0xbb)
						&&  (r_w_reg != 0x0a) 	)
                    {
                        GPIO->INTSTR |= (1 << 6);
                      	GPIO->INTPOL = (2 << (6 << 1));
                        return ;
                    }

                    sUDF_global.start_f = 0;

                    WAIT_IIC_SCL_LOW;
                    SDA_IN
                    WAIT_IIC_SCL_HIGH;

                    if (GET_SDA_DAT)        //如果是写，锁存第一个bit
                        sda_bit_val = 0x01;
                    else
                        sda_bit_val = 0x00;

										
					time2=36;               //个人理解判断Repeated Start
					GPIO->INTSTR|=(1<<6);
					while((SCL==1)&&(time2--))
					{
						WDT->STR=0x01;
						if((GPIO->INTSTR&(1<<6))!=0)
						{
							sUDF_global.start_f=1;
							GPIO->INTSTR|=(1<<6);
							break;
						}				
					}
					if(time2==0)
					{
						sUDF_global.start_f=1;
					}										
										
                    if (sUDF_global.start_f == 1) //确保start有效
                    {
                        for (int i = 0; i < 2; i++); // udsf_delay_us(1);
                        if (SCL == 1)
                            sUDF_global.start_f = 1;
                        else
                            sUDF_global.start_f = 0;
                    }

                    if (sUDF_global.start_f == 1) //read
                    {
                        receive_byte = 0;
                        for (bitcount = 0; bitcount < 8; bitcount ++) //wait second device addr
                        {
                            WAIT_IIC_SCL_LOW;
                            SDA_IN;//输入
                            WAIT_IIC_SCL_HIGH;
                            receive_byte <<= 1;  //先移位，再读数

                            receive_byte |= GET_SDA_DAT;
                        }
                        IIC_SLAVE_SEND_ACK

                        //if (r_w_reg <= 3)                           //send data
                        //{
						unsigned char txmask = 0x80;
						unsigned char val = 0;
						uint8_t count = 0;					
						if (r_w_reg == 16)
							val = sUDF_global.i2c_reg10;
						else if (r_w_reg == 17)
							val = sUDF_global.i2c_reg11;
						else if (r_w_reg == 18)
							val = sUDF_global.i2c_reg12;
						else if (r_w_reg == 19)
							val = sUDF_global.i2c_reg13;
						else if (r_w_reg == 49)
							val = sUDF_global.i2c_reg31;
						else if (r_w_reg == 50)
							val = sUDF_global.i2c_reg32;
						else if (r_w_reg == 62)
							val = sUDF_global.i2c_reg3e;
						else if (r_w_reg == 63)
							val = sUDF_global.i2c_reg3f;												
						else if (r_w_reg == 65)
							val = sUDF_global.i2c_reg41;
						else if (r_w_reg == 66)
							val = sUDF_global.i2c_reg42;
						else if (r_w_reg == 67)
							val = sUDF_global.i2c_reg43;
//						else if (r_w_reg == 68)
//							val = sUDF_global.i2c_reg44;
						else if (r_w_reg == 69)
							val = sUDF_global.i2c_reg45;
//						else if (r_w_reg == 78)
//							val = sUDF_global.i2c_reg4e;
//						else if (r_w_reg == 79)
//							val = sUDF_global.i2c_reg4f;
						else if (r_w_reg == 92)
							val = sUDF_global.i2c_reg5c;
						else if (r_w_reg == 0)
							val = sUDF_global.i2c_reg00;
//						else if (r_w_reg == 103)
//							val = sUDF_global.i2c_reg67;
//						else if (r_w_reg == 104)
//							val = sUDF_global.i2c_reg68;
						else if (r_w_reg == 0xaa)
							val = sUDF_global.i2c_regaa;
						else if (r_w_reg == 0x0a)
							val = sUDF_global.version[count];

						
						for (bitcount = 0; bitcount < 8; bitcount ++)
						{
							WAIT_IIC_SCL_LOW;
							SDA_OUT;
							if (val & txmask)
									SDA = 1;
							else
									SDA = 0;
							WAIT_IIC_SCL_HIGH;
		
							txmask = txmask >> 1;
						}
						WAIT_IIC_SCL_LOW;
						SDA_IN;
						WAIT_IIC_SCL_HIGH;
						while(r_w_reg == 0x0a && GET_SDA_DAT == 0){
							count++;
							txmask = 0x80;
							val = sUDF_global.version[count];
							for (bitcount = 0; bitcount < 8; bitcount ++)
							{
								WAIT_IIC_SCL_LOW;
								SDA_OUT;
								if (val & txmask)
									SDA = 1;
								else
									SDA = 0;
								WAIT_IIC_SCL_HIGH;
		
								txmask = txmask >> 1;
						}
							
							WAIT_IIC_SCL_LOW;
							SDA_IN;
							WAIT_IIC_SCL_HIGH;
						}

							udsf_delay_ms(1);//过滤掉通讯尾端的SDA下降沿
                        //}
                    }
					else //write
                    {
                        WDT->STR = 0x1;
                        receive_byte = 0;

                        receive_byte |= sda_bit_val; //MSB

                        //if (r_w_reg <= 3)
                        //{
						for (bitcount = 0; bitcount < 7; bitcount ++)
						{
								WAIT_IIC_SCL_LOW;
								SDA_IN;
								WAIT_IIC_SCL_HIGH;
								receive_byte <<= 1;  //先移位，再读数

								receive_byte |= GET_SDA_DAT;
						}
						if (r_w_reg == 0x10){
							sUDF_global.i2c_reg10 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
							
						else if (r_w_reg == 0x11){
							sUDF_global.i2c_reg11 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x12){
							sUDF_global.i2c_reg12 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x13){
							sUDF_global.i2c_reg13 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x31){
							sUDF_global.i2c_reg31 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x32){
							sUDF_global.i2c_reg32 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x3e){
							sUDF_global.i2c_reg3e = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x3f){
							sUDF_global.i2c_reg3f = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
//						else if (r_w_reg == 0x41){
//							sUDF_global.i2c_reg41 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
//						else if (r_w_reg == 0x42){
//							sUDF_global.i2c_reg42 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
//						else if (r_w_reg == 0x43){
//							sUDF_global.i2c_reg43 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
//						else if (r_w_reg == 0x44){
//							sUDF_global.i2c_reg44 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
						else if (r_w_reg == 0x45){
							sUDF_global.i2c_reg45 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
//						else if (r_w_reg == 0x4e){
//							sUDF_global.i2c_reg4e = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
//						else if (r_w_reg == 0x4f){
//							sUDF_global.i2c_reg4f = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
						else if (r_w_reg == 0x5c){
							sUDF_global.i2c_reg5c = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
						else if (r_w_reg == 0x00){
							sUDF_global.i2c_reg00 = receive_byte;
							IIC_SLAVE_SEND_ACK;
						}
//						else if (r_w_reg == 0x67){
//							sUDF_global.i2c_reg67 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
//						else if (r_w_reg == 0x68){
//							sUDF_global.i2c_reg68 = receive_byte;
//							IIC_SLAVE_SEND_ACK;
//						}
						else if (r_w_reg == 0xaa){
							sUDF_global.i2c_regaa = receive_byte;
							IIC_SLAVE_SEND_ACK;
							if(receive_byte == 0xf2 || receive_byte == 0xf3 || receive_byte == 0xf1|| receive_byte == 0xf4){
								return;
							}
						}
						else if	(r_w_reg == 0xbb  && sUDF_global.i2c_regaa != 0xF0)
						{
							
							uint16_t count = 0;
							sUDF_global.i2c_regbb[count] = receive_byte;
							IIC_SLAVE_SEND_ACK;
							
//							while(1)
//							{														
//								if (GET_SDA_DAT)       					//如果是写，锁存第一个bit
//									sda_bit_val = 0x01;
//								else
//									sda_bit_val = 0x00;
//								time2=36;								//同个人理解判断Stop
//								GPIO->INTPOL = (0 << (6 << 1));			//改为上升沿触发
//								GPIO->INTSTR|=(1<<6);
//								while((SCL==1)&&(time2--))
//								{
//									WDT->STR=0x01;
//									if((GPIO->INTSTR&(1<<6))!=0)
//									{
//										sUDF_global.stop_f=1;
//										GPIO->INTSTR|=(1<<6);
//										break;											
//									}				
//								}		
//								if(time2==0)
//								{
//									sUDF_global.stop_f=1;
//								}								
//								if (sUDF_global.stop_f == 1) //确保stop有效
//								{
//									for (int i = 0; i < 2; i++); // udsf_delay_us(1);
//									if (SCL == 1)
//									{	
//										sUDF_global.stop_f = 1;
//										sUDF_global.i2c_regaa = 0x02;
//									}
//									else
//										sUDF_global.stop_f = 0;
//								}
//								if(sUDF_global.stop_f == 0)
//								{
								for(uint8_t i=0;i<127;i++){
										receive_byte = 0;
//									receive_byte |= sda_bit_val; //MSB
									for (bitcount = 0; bitcount < 8; bitcount ++)
									{
										WAIT_IIC_SCL_LOW;
										SDA_IN;
										WAIT_IIC_SCL_HIGH;
										receive_byte <<= 1;  //先移位，再读数
										receive_byte |= GET_SDA_DAT;
										
									}
									count++;									
									sUDF_global.i2c_regbb[count] = receive_byte;									
									IIC_SLAVE_SEND_ACK;
									//sUDF_global.i2c_regaa = sUDF_global.binpage_cnt;
								}
								sUDF_global.binpage_cnt++;
								return;

								//}else{break;}														
							//}
						}
						WAIT_IIC_SCL_LOW;
						SDA_IN;

           }
			}
//                udsf_delay_ms(1);//过滤掉通讯尾端的SDA下降沿
                GPIO->INTSTR |= (1 << 6);
                sUDF_global.start_f = 0;
//								sUDF_global.stop_f = 0;
               	if((sUDF_global.i2c_reg00 == 1)&&(r_w_reg == 0))
								{
//                  udsf_spiflash_erase_sector(IIC_Para_ADDR);
//                  sUDF_global.state=0;
									return;
								}
            
					}
            time--;
            if (time == 0)
            {
                GPIO->INTSTR |= (1 << 6);
                sUDF_global.start_f = 0;
							sUDF_global.stop_f = 0;
            }
///**/
        }
				GPIO->INTPOL = (2 << (6 << 1));
    
	}
    else
    {
        //没发现下降沿，继续执行正常代码
    }

	

}
#endif
